<?php
// +----------------------------------------------------------------------
// | 计划任务
// +----------------------------------------------------------------------

class cron_controller extends base_controller {

	public function _init(){
		if(!C('iscron')) exit("cron doesn't open!");
		$this->chkCmd();
	}

	//### 每周计划任务运行 ###
	//定期处理关键词搜索结果数
	public function setKeywordsCount() {
		$time1 = time();
		$key = M('keywords');

		$tmpTable = 'keywords_tmp';
		$key->execute("DROP TABLE IF EXISTS " . $tmpTable);
		$key->execute("CREATE TABLE " . $tmpTable . " like keywords");
		$key->execute("INSERT " . $tmpTable . " select * from keywords");

		$key2 = M($tmpTable);
		$count = $key2->count();
		if(empty($count)){
			exit('Error: www.data.setKeywordsCount');
		}
		for ($i = 0; $i <= $count; $i = $i + 1000) {
			$rows = $key2->field("id,name")->limit("$i,1000")->order("id asc")->findAll();
			foreach ($rows as $v) {
				$num = M('image')->where("match(name) against('{$v['name']}')")->count();
				$key2->where("id='{$v['id']}'")->save(array('search_num'=>(int)$num));
				echo 'keyword_id:'.$v['id'].' |  search_num:'.$num."\n";
			}
		}

		$key->execute("REPAIR TABLE " . $tmpTable);
		$key->execute("DROP TABLE keywords");
		$key->execute("RENAME TABLE " . $tmpTable . " TO `keywords`");
		echo "www.cron.setKeywordsCount run ok!!\n";
		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";
	}

	//### 每日计划任务运行 ###
	//定期更新分类缩略图、图片总数、分类总浏览量
	public function setCategoryImage(){
		$time1 = time();
		
		$cate = M('category');
		$rows = $cate->order("id asc")->findAll();
		$ids = array();
		foreach($rows as $v){
			//获取图片ID
			$map = $v['pid']==0 ? "catid1={$v['id']}" : "catid2={$v['id']}";
			$id = M('image')->where($map)->order("rand()")->getField('id');
			if($ids && in_array($id,$ids)){
				for($i=0;$i<=20;$i++){
					$id = M('image')->where($map)->order("rand()")->getField('id');
					if(!in_array($id,$ids)){
						continue;
					}
				}
			}
			$ids[] = $id;

			//获取图片总数
			$items = M('image')->where($map)->count();

			//获取总浏览量
			$view = M('image')->where($map)->getField('sum(view) as count_view');

			//更心分类
			$cate->where("id='{$v['id']}'")->save(array('image_id'=>$id, 'items'=>$items, 'view'=>$view));

			echo "{$v['id']}:{$v['name']}\n";

		}
		$cate->cacheDelete();
		echo "www.cron.setCategoryImage run ok!!";

		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";
	}

	

	//### 每月计划任务图片生成和清理 ###
	//图片缩略图处理
	public function setThumb() {
		$time1 = time();
		$rows = C('piexl');
		$pixs = array();
		foreach($rows as $v){
			$pixs = array_merge($pixs,$v);
		}
		$pixs[] = '160_120';
		$pixs[] = '480_360';
		
		$count = $this->image->count();
		for ($i = 0; $i <= $count; $i = $i + 3) {
			$lists = $this->image->field('id')->order('id asc')->limit("$i,3")->findAll();
			if(empty($lists))  break;

			foreach ($lists as $v) {
				$f1 = imgurl($v['id'],'',true);
				if (!is_file($f1) || filesize($f1) < 2048) continue;

				//生成各种分辨率图片
				foreach ($pixs as $v2) {
					$thumb = imgurl($v['id'],$v2,true);
					$pix = explode('_', $v2);
					image::thumb($f1, $thumb, $pix[0], $pix[1], 1, '80');
					echo $v['id'] . ' ' . $thumb . "\n";
				}
			}
		}

		$time2 = time()-$time1;
		echo "\n==================  run time: $time2 seconds  ==================\n";
	}

}